package cz.muni.fi.pa036is.servicesimplementation;

import cz.muni.fi.pa036is.daointerface.CommentDAO;
import cz.muni.fi.pa036is.daointerface.ComplaintDAO;
import cz.muni.fi.pa036is.daointerface.MatchDAO;
import cz.muni.fi.pa036is.daointerface.PersonDAO;
import cz.muni.fi.pa036is.daointerface.RoundDAO;
import cz.muni.fi.pa036is.daointerface.SeasonDAO;
import cz.muni.fi.pa036is.dto.MatchBasicTO;
import cz.muni.fi.pa036is.dto.MatchDetailTO;
import cz.muni.fi.pa036is.dto.MatchFormTO;
import cz.muni.fi.pa036is.dto.MatchGridTO;
import cz.muni.fi.pa036is.dto.SeasonBasicTO;
import cz.muni.fi.pa036is.dtoconverters.MatchConverter;
import cz.muni.fi.pa036is.dtoconverters.SeasonConverter;
import cz.muni.fi.pa036is.entity.Comment;
import cz.muni.fi.pa036is.entity.Complaint;
import cz.muni.fi.pa036is.entity.Match;
import cz.muni.fi.pa036is.entity.Person;
import cz.muni.fi.pa036is.entity.Round;
import cz.muni.fi.pa036is.entity.Season;
import cz.muni.fi.pa036is.servicesinterface.MatchService;
import cz.muni.fi.pa036is.utilities.ScoreCalculationService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Shebik
 */
@Service
public class MatchServiceImpl implements MatchService {

    @Autowired
    private MatchDAO matchDAO;
    @Autowired
    private RoundDAO roundDAO;
    @Autowired
    private CommentDAO commentDAO;
    @Autowired
    private ComplaintDAO complaintDAO;
    @Autowired
    private SeasonDAO seasonDAO;
    @Autowired
    private PersonDAO personDAO;
    @Autowired
    private ScoreCalculationService scoreService;

    @Override
    public void addMatch(MatchFormTO matchFormTO) {
        
        Match match = MatchConverter.convertToMatch(matchFormTO);
        
        matchDAO.addMatch(match);
        matchFormTO.setId(match.getId());
    }

    @Override
    public void editMatch(MatchFormTO matchFormTO) {
        matchDAO.editMatch(MatchConverter.convertToMatch(matchFormTO));
    }

    @Override
    public void deleteMatch(Long id) {
        for (final Round round : roundDAO.getAllRoundsForMatch(id)) {
            roundDAO.deleteRound(round.getMatchId(), round.getSequenceNumber());
        }
        for (final Comment comment : commentDAO.getAllCommentsForMatch(id)) {
            commentDAO.deleteComment(comment.getId());
        }
        for (final Complaint complaint : complaintDAO.getAllComplaintsForMatch(id)) {
            complaintDAO.deleteComplaint(complaint.getId());
        }
        matchDAO.deleteMatch(id);
    }

    @Override
    public MatchFormTO getMatchForm(Long id) {
        final MatchFormTO matchFormTO =
                MatchConverter.convertToMatchFormTO(matchDAO.findMatch(id));
        final SeasonBasicTO seasonBasicTO = SeasonConverter.convertToSeasonBasicTO(
                seasonDAO.findSeason(matchFormTO.getId()));
        matchFormTO.setSeason(seasonBasicTO);
        return matchFormTO;
    }

    @Override
    public MatchDetailTO getMatchDetail(Long id) {
        final MatchDetailTO matchDetailTO =
                MatchConverter.convertToMatchDetailTO(matchDAO.findMatch(id));
        final SeasonBasicTO seasonBasicTO = SeasonConverter.convertToSeasonBasicTO(
                seasonDAO.findSeason(matchDetailTO.getId()));
        matchDetailTO.setSeason(seasonBasicTO);
        return matchDetailTO;
    }

    @Override
    public List<MatchGridTO> getMatchesBySeason(Long seasonId) {

        Season season = this.seasonDAO.findSeason(seasonId);

        List<MatchGridTO> matches = new ArrayList<>();
        for (Match m : this.matchDAO.getAllMatchesForSeason(seasonId)) {

            Person player1 = this.personDAO.findPerson(m.getPlayer1Id());
            Person player2 = this.personDAO.findPerson(m.getPlayer2Id());

            String finalScore = this.scoreService.calculateFinalScore(
                    m.getId(),
                    season,
                    this.roundDAO.getAllRoundsForMatch(m.getId()));

            MatchGridTO match = MatchConverter.convertToMatchGridTO(
                    m,
                    season,
                    player1,
                    player2,
                    finalScore);

            matches.add(match);
        }

        return matches;
    }

    @Override
    public MatchBasicTO getMatchBasic(Long id) {

        Match match = this.matchDAO.findMatch(id);
        return MatchConverter.convertToMatchBasicTO(match);

    }
}
